﻿using System.Collections.Generic;
using SymbolIntegration.core.Expressions;

namespace SymbolIntegration.core.Helpers
{
    public class MatrixHelper
    {
        public List<List<Expression>> GetMinor(int row, int col, List<List<Expression>> exprList)
        {
            var result = new List<List<Expression>>();
            for (int i = 0; i < exprList.Count; i++)
            {
                if (row != i)
                {
                    result.Add(new List<Expression>());
                    for (int j = 0; j < exprList.Count; j++)
                    {
                        if (col != j)
                        {
                            result[i].Add(exprList[i][j]);
                        }
                    }
                }
            }
            return result;
        }

        public Expression GetDeterminant(List<List<Expression>> exprList)
        {
            if (exprList.Count == 1)
            {
                return exprList[0][0];
            }

            Expression result = new ConstantExpression(0);
            for (int i = 0; i < exprList.Count; i++)
            {
                for (int j = 0; j < exprList.Count; j++)
                {
                    int additionalMult = ((i + j) % 2 == 0) ? 1 : -1;
                    result = result + GetDeterminant(GetMinor(i, j, exprList)) * new ConstantExpression(additionalMult);
                }
            }
            return result.Normalize();
        }

        public List<List<Expression>> GetInvite(List<List<Expression>> exprList)
        {
            var result = new List<List<Expression>>();
            for (int j = 0; j < exprList.Count; j++)
            {
                result.Add(new List<Expression>());
                for (int i = 0; i < exprList.Count; i++)
                {
                    result[j].Add(GetDeterminant(GetMinor(j, i, exprList)));
                }
            }

            return result;
        }

        public List<Expression> Mul(List<List<Expression>> exprList, List<Expression> exprVector)
        {
            for (int i = 0; i < exprVector.Count; i++)
            {
                for (int j = 0; j < exprVector.Count; j++)
                {
                    //TODO: no need
                }
            }
            return null;
        }
    }
}
